package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * CategoryController
 *
 * @author liliudong
 * @version 1.0
 * @description 分类控制器
 * @date 2023/7/27 20:32
 */
@RestController
@RequestMapping("/admin/category")
@Slf4j
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    /**
     * 创建
     *
     * @param categoryDTO 类别dto
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping
    public Result<?> create(@RequestBody CategoryDTO categoryDTO) {
        categoryService.create(categoryDTO);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param categoryPageQueryDTO 类别页面查询dto
     * @return {@link Result}<{@link PageResult}>
     */
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult pageResult = categoryService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 更新通过id
     *
     * @param categoryDTO 类别dto
     * @return {@link Result}<{@link ?}>
     */
    @PutMapping
    public Result<?> updateById(@RequestBody CategoryDTO categoryDTO) {
        categoryService.updateById(categoryDTO);
        return Result.success();
    }

    /**
     * 更新状态
     *
     * @param id     id
     * @param status 状态
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/status/{status}")
    public Result<?> updateStatus(Long id, @PathVariable Integer status) {
        categoryService.updateStatus(id, status);
        return Result.success();
    }

    /**
     * 删除通过id
     *
     * @param id id
     * @return {@link Result}<{@link ?}>
     */
    @DeleteMapping
    public Result<?> deleteById(Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 根据类型列表
     *
     * @param type 类型
     * @return {@link Result}<{@link List}<{@link Category}>>
     */
    @GetMapping("/list")
    public Result<List<Category>> listByType(Integer type) {
        List<Category> categoryList = categoryService.listByType(type);
        return Result.success(categoryList);
    }
}
